package com.mocne.Common;

import com.alibaba.fastjson.JSONObject;
import com.csvreader.CsvReader;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;

public class ReadCSVFile {

    public static ArrayList<String[]> readCsvFile(String filePath) throws Exception {
        ArrayList<String[]> csvList = new ArrayList<>();
        CsvReader reader = new CsvReader(filePath, ',', StandardCharsets.UTF_8);
        reader.readHeaders(); //跳过表头
        while (reader.readRecord()) {
            csvList.add(reader.getValues()); //按行读取，并把每一行的数据添加到list集合
        }
        reader.close();
        System.out.println("读取的行数：" + csvList.size());
        System.out.println("读取的列数：" + csvList.get(0).length);
        ArrayList<String> titleList = new ArrayList<>();
        titleList.add("case");
        titleList.add("domain");
        titleList.add("port");
        titleList.add("method");
        titleList.add("api");
        titleList.add("param");
        titleList.add("body");
        JSONObject jsonObject = new JSONObject();

        ArrayList<JSONObject> caseList = new ArrayList<>();
        for (String[] strings : csvList) {
            for (int j = 0; j < strings.length; j++) {
                jsonObject.put(titleList.get(j), strings[j]);

            }
            caseList.add(jsonObject);
        }

        return csvList;
    }

    public static Object[][] readCSV2Provider(String filePath) {
        ArrayList<String[]> dataAim;
        try {
            dataAim = readCsvFile(filePath);
        } catch (Exception e) {
            e.printStackTrace();
            return new Object[][]{};
        }

        HashMap<String, String>[][] testData = new HashMap[dataAim.size() - 1][1];

        // 对数组中所有元素hashmap进行初始化
        for (int i = 0; i < dataAim.size() - 1; i++) {
            testData[i][0] = new HashMap<>();
        }

        return testData;
    }

    public static void main(String[] args) throws Exception {

        Object[][] test = readCSV2Provider("src/main/resources/Data.csv");

        String filePath = "src/main/resources/Data.csv";
        ArrayList<String[]> dataAim = readCsvFile(filePath);
        System.out.println(dataAim.size());
    }

}
